package leetcode.offer;

/**
 * ClassName: Offer33_VerifyPostorder
 * Description:leetcode-剑指offer 33 二叉搜索树的后序遍历序列
 * date: 2021-11-15 22:58
 *
 * @author liyifan
 */
public class Offer33_VerifyPostorder {
    public boolean verifyPostorder(int[] postorder){
        return recur(postorder, 0, postorder.length - 1);
    }

    boolean recur(int[] postorder, int i, int j) {
        if (i >= j)
            return true;
        int p = i;
        while (postorder[p] < postorder[j])
            p++;
        int m = p;
        while (postorder[p] > postorder[j])
            p++;
        return p == j && recur(postorder, i, m - 1) && recur(postorder, m, j - 1);
    }
}
